import { uploadUrl, baseUrl } from './config.js';
const token = wx.getStorageSync("token")
/**
 * 普通请求
 */
const request = function(url, method, param){
  return new Promise(function(resolve, reject){
    wx.request({
      url: `${baseUrl}${url}`, //仅为示例，并非真实的接口地址
      data: param,
      method: method,
      header: {
        'content-type': 'application/json;charset=utf-8', // 默认值
        'authorization': token
      },
      success: function (res) {
        resolve(res.data);
      },
      fail: function(err){
        reject(err);
      }
    })
  })
  
}
const upload = function(filePath, param){
  return new Promise(function (resolve, reject) {
    wx.uploadFile({
      header: {
        'content-type': 'application/json;charset=utf-8', // 默认值
        'authorization': token
      },
      url: `${uploadUrl}v0/file/upload`, //仅为示例，非真实的接口地址
      filePath: filePath,
      name: 'file',
      formData: param,
      success: function (res) {
        var data = res.data
        try{
          resolve(JSON.parse(data));
        }catch(e){
          reject(e);
        }
      },
      fail: function(err){
        reject(err);
      }
    })
  });
}
export { request, upload };